<template>
  <el-container class="hoome-container">
    <!-- 头部区域 -->
    <el-header
      ><el-button type="info" @click="logout">退出</el-button></el-header
    >
    <!-- 主体内容区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <!-- 侧边栏区域 -->
        <div class="toggle" @click="togglecolse">|||</div>
        <el-menu
          unique-opened
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#1E90FF"
          :collapse="iscollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span> {{ item.authName }} </span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="save('/' + subitem.path)"
            >
              <!-- 二级菜单的模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subitem.authName }} </span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      // 是否折叠
      iscollapse: false,
      // 被激活的地址
      activePath: '',
      // 图标数据
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activepath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('./login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.menulist = res.data
    },
    // 折叠菜单
    togglecolse() {
      this.iscollapse = !this.iscollapse
    },
    // 保存连接的激活状态
    save(activepath) {
      window.sessionStorage.setItem('activepath', activepath)
      this.activePath = activepath
    }
  }
}
</script>

<style>
.iconfont {
  margin-right: 10px;
}
.el-header {
  display: flex;
  justify-content: flex-end;
  background-color: #303133;
  align-items: center;
  font-size: 20px;
}
.el-aside {
  background-color: #303133;
}
.el-main {
  background-color: #f5f5f5;
}
.hoome-container {
  height: 100%;
}
.el-menu {
  border-right: none;
}
.toggle {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
